package com.ybxiang.camera_opengl

import android.content.Context
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import android.view.SurfaceHolder

/**
 * Created by yunbo.xiang on 2021/12/15
 * Describe:
 */
class CameraView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
) : GLSurfaceView(context, attrs) {
    private var mRender: CameraRender

    init {
        // 1、使用OpenGL ES 2.0
        setEGLContextClientVersion(2)
        // 2、设置渲染回调接口
        mRender = CameraRender(this)
        setRenderer(mRender)
        /**
         * 3、设置刷新方式
         * 1、RENDERMODE_WHEN_DIRTY 手动刷新，调用requestRender()
         * 2、RENDERMODE_CONTINUOUSLY 自动刷新，大概16ms自动回调一次onDraw方法
         */
        // 注意：必须在setRender方法后面
        renderMode = RENDERMODE_WHEN_DIRTY
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        super.surfaceDestroyed(holder)
        mRender.onSurfaceDestroyed()
    }
}